package com.ycsj.goldmedalnewconcept.teacher.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ycsj.common.utils.ScreenUtils;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.teacher.bean.ClassInfo;

import java.util.List;

/**
 * 显示班级的GridView的适配器
 * Created by Administrator on 2019/3/22 0022.
 */

public class ClassGradeGvAdapter extends BaseAdapter {

    private List<ClassInfo.ListBean> datas;

    private Context context;

    public ClassGradeGvAdapter(Context context, List<ClassInfo.ListBean> datas) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (datas.size() >= 10) {
            return 10;
        } else {
            return datas.size() + 1;
        }
    }

    @Override
    public Object getItem(int pos) {
        return pos;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.gv_item_class_grade, null);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //动态计算条目的宽度,注意这里转化成px
        int itemWidth = (ScreenUtils.getScreenWidth(context) - UiUtils.dp2px(8 * 6)) / 3;

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(itemWidth, itemWidth * 3 / 5);
        holder.tvShowClass.setLayoutParams(lp);

        //做动态处理,根据返回的图片途径来显示图片
        if (datas.size() == 10) {
            //只显示图片,不显示加号
            holder.tvShowClass.setBackgroundResource(R.drawable.class_gv_bg);
            ClassInfo.ListBean info = datas.get(position);
            //设置班级名称
            holder.tvShowClass.setText(info.getName());
        } else {

            //显示图片和加号,最后一个位置显示+号
            if (position != datas.size()) {
                //不是最后一个位置
                holder.tvShowClass.setBackgroundResource(R.drawable.class_gv_bg);

                ClassInfo.ListBean info = datas.get(position);

                //设置班级名称
                holder.tvShowClass.setText(info.getName());
            } else {
                //最后一个位置
                holder.tvShowClass.setBackgroundResource(R.drawable.class_gv_bg_add);
                holder.tvShowClass.setText("");
            }
        }


        holder.tvShowClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (datas.size() == 10) {
                    //跳转到班级信息
                    if (onTvClickListener != null) {
                        onTvClickListener.goClassInfo(datas.get(position));
                    }
                } else {
                    if (position == datas.size()) {
                        //最后一个+号的点击事件,添加班级
                        if (onTvClickListener != null) {
                            onTvClickListener.addClass();
                        }

                    } else {
                        //跳转到班级信息
                        if (onTvClickListener != null) {
                            onTvClickListener.goClassInfo(datas.get(position));
                        }
                    }
                }

            }
        });

        return convertView;
    }


    static class ViewHolder {
        TextView tvShowClass;

        ViewHolder(View view) {
            tvShowClass = view.findViewById(R.id.tv_show_class);
            view.setTag(this);
        }
    }


    /**
     * 点击事件回调接口
     */
    public OnTvClickListener onTvClickListener;

    public interface OnTvClickListener {
        void addClass();

        void goClassInfo(ClassInfo.ListBean listBean);
    }

    public void setOnTvClickListener(OnTvClickListener onTvClickListener) {
        this.onTvClickListener = onTvClickListener;
    }
}
